package com.cj.mobile.iabp.mvp.ui.fragment;

import static com.jess.arms.utils.Preconditions.checkNotNull;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.di.component.DaggerHomeNewComponent;
import com.cj.mobile.iabp.mvp.contract.HomeNewContract;
import com.cj.mobile.iabp.mvp.model.entity.DepotHomeResponse;
import com.cj.mobile.iabp.mvp.presenter.HomeNewPresenter;
import com.cj.mobile.iabp.mvp.ui.activity.AddPatientActivity;
import com.cj.mobile.iabp.mvp.ui.activity.HomePatientActivity;
import com.cj.mobile.iabp.mvp.ui.activity.HomeSearchActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 首页
 * <p>
 * Created on 2023/11/15 17:19
 *
 * @author 赤槿
 * module name is HomeNewActivity
 */
public class HomeNewFragment extends BaseFragment<HomeNewPresenter> implements HomeNewContract.View {
    /*-------------------------------------------控件信息-------------------------------------------*/
    @BindView(R.id.fragment_home_new)
    NestedScrollView contentLayout;                                                                 // 主布局

    @BindView(R.id.txvi_homenew_allpatient)
    TextView txviAllPatient;                                                                        // 所有患者
    @BindView(R.id.lila_homenew_allleft_num)
    TextView txviCYYQNum;                                                                           // 朝阳院区
    @BindView(R.id.lila_homenew_allright_num)
    TextView txviTZYQNum;                                                                           // 通州院区
    @BindView(R.id.txvi_homenew_mypatient)
    TextView txviMyPatient;                                                                         // 我的患者

    /*-------------------------------------------业务信息-------------------------------------------*/
    @Inject
    AccountManager mAccountManager;

    /**
     * 对话框
     */
    private MaterialDialog mDialog;

    @Override
    public void onResume() {
        super.onResume();

        if (mPresenter != null) {
            mPresenter.initData();
        }
    }

    @Override
    public void onDestroy() {
        if (mDialog != null) {
            this.mDialog.dismiss();
        }
        super.onDestroy();
        this.mDialog = null;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerHomeNewComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_new, container, false);
    }

    /**
     * 在 onActivityCreate()时调用
     */
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        // 初始化Loading对话框
        mDialog = new MaterialDialog.Builder(getContext()).content(R.string.common_execute).progress(true, 0).cancelable(false).build();

    }

    @OnClick({
            R.id.lila_homenew_search,                                                               // 搜索
            R.id.cola_homenew_addpatient,                                                           // 添加患者

            R.id.lila_homenew_xyx_cy,                                                               // 现运行 - 朝阳
            R.id.lila_homenew_xyx_tz,                                                               // 现运行 - 通州

            R.id.lila_homenew_gz_cy,                                                                // 关注 - 朝阳
            R.id.lila_homenew_gz_tz,                                                                // 关注 - 通州

            R.id.lila_homenew_dgd_cy,                                                               // 待归档 - 朝阳
            R.id.lila_homenew_dgd_tz,                                                               // 待归档 - 通州
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lila_homenew_search:                                                          // 搜索
                ActivityUtils.startActivity(HomeSearchActivity.class);

                break;
            case R.id.cola_homenew_addpatient:                                                      // 添加患者
                ActivityUtils.startActivity(AddPatientActivity.class);
                break;

            case R.id.lila_homenew_xyx_cy:                                                          // 现运行 - 朝阳
                onJumpPatients("2", "0");
                break;
            case R.id.lila_homenew_xyx_tz:                                                          // 现运行 - 通州
                onJumpPatients("3", "0");
                break;

            case R.id.lila_homenew_gz_cy:                                                           // 关注 - 朝阳
                onJumpPatients("2", "1");
                break;
            case R.id.lila_homenew_gz_tz:                                                           // 关注 - 通州
                onJumpPatients("3", "1");
                break;

            case R.id.lila_homenew_dgd_cy:                                                          // 待归档 - 朝阳
                onJumpPatients("2", "2");
                break;
            case R.id.lila_homenew_dgd_tz:                                                          // 待归档 - 通州
                onJumpPatients("3", "2");
                break;
        }
    }

    /**
     * 跳转到(现运行、关注、待归档)信息记录界面
     *
     * @param deptSec 院区(朝阳2；通州3；)
     * @param state   状态
     */
    private void onJumpPatients(String deptSec, String state) {
        Bundle mBundle = new Bundle();
        mBundle.putString("deptSec", deptSec);
        mBundle.putString("state", state);
        ActivityUtils.startActivity(mBundle, HomePatientActivity.class);
    }

    /**
     * 双击刷新
     */
    public void onDoubleClick() {
        if (mPresenter != null) {
            mPresenter.initData();
        }
    }

    /**
     * 提交时显示loading框
     */
    @Override
    public void showLoadingSubmit() {
        if (mDialog != null)
            mDialog.show();
    }

    /**
     * 提交后隐藏loading框
     */
    @Override
    public void hideLoadingSubmit() {
        if (mDialog != null)
            mDialog.dismiss();
    }

    @Override
    public void loadHomeInfo(DepotHomeResponse info) {
        if (info != null) {
            txviAllPatient.setText("所有患者：" + info.getAll());
            txviCYYQNum.setText(String.valueOf(info.getSec_2()));
            txviTZYQNum.setText(String.valueOf(info.getSec_3()));
            txviMyPatient.setText("我安装IABP的患者：" + info.getMy());
        }
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    public Fragment getFragment() {
        return this;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }
}